package com.practice.controller;

import com.alibaba.fastjson2.JSON;
import com.practice.Valid和Validated校验.User;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
/**
 * Created by IntelliJ IDEA2021.3
 * @Author: Tenghw
 * @Date: 2022/11/09  18:38
 * @Description:
 */
@Api(tags = "校验测试对外接口")
@RestController
public class ValidAndValidatedController {

    @ApiOperation(value = "测试ValidAndValidated")
    @ApiImplicitParam(name = "user",value = "用户",required = true)
    @PostMapping(value = "/getUser",consumes = {"application/json;charset=UTF-8"})
    public String getUser(@RequestBody @Valid User user){
        return JSON.toJSONString(user);
    }

    @ApiOperation(value = "测试ValidAndValidated")
    @ApiImplicitParam(name = "user",value = "用户",required = true)
    @PostMapping(value = "/getUser1",consumes = {"application/json;charset=UTF-8"})
    public String getUser1(@RequestBody @Valid User user, BindingResult bindingResult){
        if(bindingResult.hasErrors()){
            String message1 = bindingResult.getFieldError().getDefaultMessage();
            String message2 = bindingResult.getAllErrors().get(0).getDefaultMessage();
            System.out.println("message1》》》"+message1);
            System.out.println("message2》》》"+message2);
            return message1;
        }
        return JSON.toJSONString(user);
    }
}
